package zyc.dao.imple;

import zyc.dao.StudentDao;
import zyc.pojo.Page;
import zyc.pojo.Student;

import java.util.List;

/**
 * @description:
 * @author: Code-zyc
 * @date: Created in 2020/12/21 9:53
 * @version: v1.0
 * @modified By:
 */
public class StudentDaoImple extends BaseDao implements StudentDao  {

    @Override
    public List<Student> getallstudents(Page page) {

        String sql="SELECT * FROM student ORDER BY sid  LIMIT ?,?";
        return selectForMore(Student.class,sql,page.getStart(),page.getSize());
    }

    @Override
    public Student getstudentByid(Integer id) {
        String sql="SELECT * FROM student where id=?";
        return selectForOne(Student.class, sql, id);
    }

    @Override
    public void deletByid(Integer id) {
        String sql="DELETE FROM student WHERE id=?";
        update(sql,id);
    }

    @Override
    public void updatestudent(Student student) {
        String sql="UPDATE student SET sid=? ,name=? ,math=?  WHERE id=?";
        update(sql,student.getSid(),student.getName(),student.getMath(),student.getId());

    }

    @Override
    public void creatStudent(Student student) {
        String sql="INSERT INTO student (sid,password,name,math,tid) VALUES(?,?,?,?,?)";
        update(sql,student.getSid(),student.getPassword(),student.getName(),student.getMath(),student.getTeacher().getId());
    }

    @Override
    public Integer getCount() {
        String sql="SELECT count(*) FROM student";
        Number value = (Number) selectSingleValue(sql);
        return value.intValue();
    }

    @Override
    public Student getstudentBysid(Integer sid) {
        String sql="select * from student where sid=?";
        return selectForOne(Student.class,sql,sid);
    }

    @Override
    public List<Student> getstudentsByname(String name) {
        String sql="SELECT * FROM  student WHERE NAME LIKE ?";
        return selectForMore(Student.class,sql,name);
    }

    @Override
    public List<Student> getstudentsBymath(Double math) {

        String sql="SELECT * FROM  student WHERE math=?";
        return  selectForMore(Student.class,sql,math);
    }

    @Override
    public List<Student> getstudentsByAsc(String type) {
        String sql=null;
        if("math".equals(type)){
            sql="SELECT * FROM student  ORDER BY math  ";
        }

        return  selectForMore(Student.class,sql);
    }

    @Override
    public List<Student> getstudentsByDesc(String type) {
        String sql=null;
        if("math".equals(type)){
            sql="SELECT * FROM student  ORDER BY math  DESC";
        }

        return  selectForMore(Student.class,sql);
    }

    @Override
    public Student getstudentBySidPass(Integer sid, String password) {
        String sql="SELECT * FROM student WHERE sid=? AND password=?";

        String sql2="SELECT t.email FROM student s LEFT JOIN teacher t ON s.tid=t.id WHERE sid=?";
        Student student = selectForOne(Student.class, sql, sid, password);
        if(student!=null){
            Object o = selectSingleValue(sql2, sid);
            student.setEmail(o+"");
        }



        return student;
    }
}
